package cn.edu.bupt.freewings.forum.front.controller;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.edu.bupt.freewings.forum.common.UserType;
import cn.edu.bupt.freewings.forum.client.rest.UserClient;
import cn.edu.bupt.freewings.forum.front.Constants;
import cn.edu.bupt.freewings.forum.front.beans.UserBean;

public class LoginController extends HttpServlet {
	@Override
	protected void service(HttpServletRequest request,
			HttpServletResponse response) throws IOException, ServletException {
		String name = request.getParameter("name");
		String password = request.getParameter("password");
		UserType.User user = new UserClient().getByName(name);
		if (user == null || !user.getPassword().equals(password)) {
			request.setAttribute(Constants.USER_NAME, name);
			request.getRequestDispatcher("loginFailed.jsp").forward(request,
					response);
		} else {
			request.getSession().setAttribute(Constants.USER, new UserBean(user));
			request.getRequestDispatcher("home").forward(request, response);
		}
	}
}
